/*
 * @Author: soon.liao
 * @Date: 2024-02-05 21:44:50
 * @LastEditors: soon.liao
 * @LastEditTime: 2024-02-18 20:49:29
 * @FilePath: \my-car\main\l9110s.c
 * @Description: NA
 * NA
 * Copyright (c) 2024 by soon.liao  , All Rights Reserved.
 */
#include "esp_log.h"
#include "esp_system.h"
#include "driver/gpio.h"

#include "l9110s.h"

static const char *TAG = "l9110s";

void motor_l9110s_init(void)
{
    ESP_LOGI(TAG, "%s", __FUNCTION__);
    gpio_reset_pin(MOTOR_LEFT_FRONT_FOR);
    gpio_set_direction(MOTOR_LEFT_FRONT_FOR, GPIO_MODE_OUTPUT);
    gpio_set_level(MOTOR_LEFT_FRONT_FOR, 0);

    gpio_reset_pin(MOTOR_LEFT_FRONT_BAK);
    gpio_set_direction(MOTOR_LEFT_FRONT_BAK, GPIO_MODE_OUTPUT);
    gpio_set_level(MOTOR_LEFT_FRONT_BAK, 0);

    gpio_reset_pin(MOTOR_LEFT_REAR_FOR);
    gpio_set_direction(MOTOR_LEFT_REAR_FOR, GPIO_MODE_OUTPUT);
    gpio_set_level(MOTOR_LEFT_REAR_FOR, 0);

    gpio_reset_pin(MOTOR_LEFT_REAR_BAK);
    gpio_set_direction(MOTOR_LEFT_REAR_BAK, GPIO_MODE_OUTPUT);
    gpio_set_level(MOTOR_LEFT_REAR_BAK, 0);

    gpio_reset_pin(MOTOR_RIGHT_FRONT_FOR);
    gpio_set_direction(MOTOR_RIGHT_FRONT_FOR, GPIO_MODE_OUTPUT);
    gpio_set_level(MOTOR_RIGHT_FRONT_FOR, 0);

    gpio_reset_pin(MOTOR_RIGHT_FRONT_BAK);
    gpio_set_direction(MOTOR_RIGHT_FRONT_BAK, GPIO_MODE_OUTPUT);
    gpio_set_level(MOTOR_RIGHT_FRONT_BAK, 0);

    gpio_reset_pin(MOTOR_RIGHT_REAR_FOR);
    gpio_set_direction(MOTOR_RIGHT_REAR_FOR, GPIO_MODE_OUTPUT);
    gpio_set_level(MOTOR_RIGHT_REAR_FOR, 0);

    gpio_reset_pin(MOTOR_RIGHT_REAR_BAK);
    gpio_set_direction(MOTOR_RIGHT_REAR_BAK, GPIO_MODE_OUTPUT);
    gpio_set_level(MOTOR_RIGHT_REAR_BAK, 0);
}

void motor_l9110s_standby(MotorPosition position)
{
    ESP_LOGI(TAG, "%s %d", __FUNCTION__, position);
    switch (position)
    {
    case LEFT_FRONT:
        /* code */
        gpio_set_level(MOTOR_LEFT_FRONT_FOR, 0);
        gpio_set_level(MOTOR_LEFT_FRONT_BAK, 0);
        break;
    case LEFT_REAR:
        /* code */
        gpio_set_level(MOTOR_LEFT_REAR_FOR, 0);
        gpio_set_level(MOTOR_LEFT_REAR_BAK, 0);
        break;
    case RIGHT_FRONT:
        /* code */
        gpio_set_level(MOTOR_RIGHT_FRONT_FOR, 0);
        gpio_set_level(MOTOR_RIGHT_FRONT_BAK, 0);
        break;
    case RIGHT_REAR:
        /* code */
        gpio_set_level(MOTOR_RIGHT_REAR_FOR, 0);
        gpio_set_level(MOTOR_RIGHT_REAR_BAK, 0);
        break;
    default:
        break;
    }
}

void motor_l9110s_foreward(MotorPosition position)
{
    ESP_LOGI(TAG, "%s %d", __FUNCTION__, position);
    switch (position)
    {
    case LEFT_FRONT:
        /* code */
        gpio_set_level(MOTOR_LEFT_FRONT_FOR, 1);
        gpio_set_level(MOTOR_LEFT_FRONT_BAK, 0);
        break;
    case LEFT_REAR:
        /* code */
        gpio_set_level(MOTOR_LEFT_REAR_FOR, 1);
        gpio_set_level(MOTOR_LEFT_REAR_BAK, 0);
        break;
    case RIGHT_FRONT:
        /* code */
        gpio_set_level(MOTOR_RIGHT_FRONT_FOR, 1);
        gpio_set_level(MOTOR_RIGHT_FRONT_BAK, 0);
        break;
    case RIGHT_REAR:
        /* code */
        gpio_set_level(MOTOR_RIGHT_REAR_FOR, 1);
        gpio_set_level(MOTOR_RIGHT_REAR_BAK, 0);
        break;
    default:
        break;
    }
}
void motor_l9110s_reversal(MotorPosition position)
{
    ESP_LOGI(TAG, "%s %d", __FUNCTION__, position);
    switch (position)
    {
    case LEFT_FRONT:
        /* code */
        gpio_set_level(MOTOR_LEFT_FRONT_FOR, 0);
        gpio_set_level(MOTOR_LEFT_FRONT_BAK, 1);
        break;
    case LEFT_REAR:
        /* code */
        gpio_set_level(MOTOR_LEFT_REAR_FOR, 0);
        gpio_set_level(MOTOR_LEFT_REAR_BAK, 1);
        break;
    case RIGHT_FRONT:
        /* code */
        gpio_set_level(MOTOR_RIGHT_FRONT_FOR, 0);
        gpio_set_level(MOTOR_RIGHT_FRONT_BAK, 1);
        break;
    case RIGHT_REAR:
        /* code */
        gpio_set_level(MOTOR_RIGHT_REAR_FOR, 0);
        gpio_set_level(MOTOR_RIGHT_REAR_BAK, 1);
        break;
    default:
        break;
    }
}

void motor_l9110s_brake(MotorPosition position)
{
    ESP_LOGI(TAG, "%s %d", __FUNCTION__, position);
    switch (position)
    {
    case LEFT_FRONT:
        /* code */
        gpio_set_level(MOTOR_LEFT_FRONT_FOR, 1);
        gpio_set_level(MOTOR_LEFT_FRONT_BAK, 1);
        break;
    case LEFT_REAR:
        /* code */
        gpio_set_level(MOTOR_LEFT_REAR_FOR, 1);
        gpio_set_level(MOTOR_LEFT_REAR_BAK, 1);
        break;
    case RIGHT_FRONT:
        /* code */
        gpio_set_level(MOTOR_RIGHT_FRONT_FOR, 1);
        gpio_set_level(MOTOR_RIGHT_FRONT_BAK, 1);
        break;
    case RIGHT_REAR:
        /* code */
        gpio_set_level(MOTOR_RIGHT_REAR_FOR, 1);
        gpio_set_level(MOTOR_RIGHT_REAR_BAK, 1);
        break;
    default:
        break;
    }
}

void motor_l9110s_control(MotorPosition position, MotorFunction function)
{
    ESP_LOGI(TAG, "%s position=%d function =%d", __FUNCTION__, position, function);
    switch (function)
    {
    case STANDBY:
        /* code */
        motor_l9110s_standby(position);
        break;
    case FOREWARD:
        /* code */
        motor_l9110s_foreward(position);
        break;
    case REVERSAL:
        /* code */
        motor_l9110s_reversal(position);
        break;
    case BRAKE:
        /* code */
        motor_l9110s_brake(position);
        break;
    default:
        break;
    }
}